import axios from "axios"
import { Toast } from "vant"

// function setUserInfo() {
//     localStorage.setItem('userInfo', JSON.stringify({
//         "id": "e9ca23d68d884d4ebb19d07889727dae",
//         "username": "admin",
//         "realname": "管理员",
//         "avatar": "https://static.jeecg.com/temp/国炬软件logo_1606575029126.png",
//         "birthday": "1986-02-01",
//         "sex": 1,
//         "email": "jeecg@163.com",
//         "phone": "18611111111",
//     }))
// }
// setUserInfo()
// 文件上传地址
export const fileuploadUrl =
  "http://14.29.206.105:2380/jeecg-boot/sys/common/upload"
// 文件回显地址
export const fileShowUrl =
  "http://14.29.206.105:2380/jeecg-boot/sys/common/static/"
// localStorage.setItem('userInfo', {
//     "id": "e9ca23d68d884d4ebb19d07889727dae",
//     "username": "admin",
//     "realname": "管理员",
//     "avatar": "https://static.jeecg.com/temp/国炬软件logo_1606575029126.png",
//     "birthday": "1986-02-01",
//     "sex": 1,
//     "email": "jeecg@163.com",
//     "phone": "18611111111",
//     "orgCode": "A01",
//     "loginTenantId": 0,
//     "orgCodeTxt": null,
//     "status": 1,
//     "delFlag": 0,
//     "workNo": "00001",
//     "post": null,
//     "telephone": null,
//     "createBy": null,
//     "createTime": "2019-06-21 17:54:10",
//     "updateBy": "admin",
//     "updateTime": "2025-05-19 16:06:43",
//     "activitiSync": 1,
//     "userIdentity": 2,
//     "departIds": "",
//     "relTenantIds": null,
//     "clientId": null,
//     "homePath": null,
//     "postText": null,
//     "bpmStatus": null,
//     "izBindThird": false
// });
/**
 * 简单的 Axios 封装方法
 * @param {string} url - 请求地址
 * @param {string} method - 请求方法 (GET, POST, PUT, DELETE)
 * @param {object} data - 请求数据 (可选)
 * @returns {Promise} - 返回 Axios 请求的 Promise 对象
 */
export const Axios = async (
  { url = "", method = "GET", data = null, headers = {}, ...others },
  callback
) => {
  // const token = localStorage.getItem('token');
  const token =
    "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwiZXhwIjoxNzY0MTg4MTM4fQ.bGXYzTprJCtC_P5wfhwLVjkN9HhjlIU-WPblL-gGzi4"
  try {
    const Headers = {
      "Content-Type": "application/json",
      "x-access-token": token,
      // 'x-access-token': `Bearer ${token}`,
      ...headers,
    }

    const params = {
      url: `http://14.29.206.105:2380/jeecg-boot${url}`,
      method,
      data,
      headers: Headers,
      ...others,
    }
    if (others.specialUrl) {
      params.url = others.specialUrl
    }
    const response = await axios(params)
    // console.log(response,"response")
    const dataResponse = response.data
    if (callback?.returnAllData) {
      return dataResponse
    }
    if (
      response.status == 200 &&
      (dataResponse.code == 200 || dataResponse.success)
    ) {
      return callback?.callbackFile?.(response) || dataResponse.result
    }
    if (
      response.status == 200 &&
      (dataResponse.code != 200 || dataResponse.success)
    ) {
      Toast(dataResponse.message || "请求失败")
      return
    }
    return Toast("请求失败,请联系管理员")
  } catch (error) {
    console.error("请求失败:", error.response?.data || error.message)
    throw error
  }
}
